package com.bw.zuoye0722.login

import androidx.lifecycle.viewModelScope
import com.bw.base.entity.LoginEntity
import com.bw.base.entity.Res
import com.bw.base.entity.SendCodeEntity
import com.bw.base.mvvm.BaseViewModel
import com.bw.base.utils.SingleLiveData
import kotlinx.coroutines.launch

class LoginViewModel:BaseViewModel() {
    val sendCodeSuccess=SingleLiveData<Res<SendCodeEntity>>()
    val sendCodeFail=SingleLiveData<String>()
    fun sendCode(phone:String){
        viewModelScope.launch {
            try {
                sendCodeSuccess.postValue(apiservice.sendCode(phone))
            }catch (e:Exception){
                sendCodeFail.postValue(e.message.toString())
            }
        }
    }

    val loginSuccess=SingleLiveData<Res<LoginEntity>>()
    val loginFail=SingleLiveData<String>()
    fun login(captcha:String,phone:String){
        viewModelScope.launch {
            try {
                var map=HashMap<String,Any>()
                map.put("captcha",captcha)
                map.put("phone",phone)
                loginSuccess.postValue(apiservice.login(mapToBady(map)))
            }catch (e:Exception){
                loginFail.postValue(e.message.toString())
            }
        }
    }
}